<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .name {
        width: 600px;
        height: 400px;
        margin: 50px auto;
        border: 1px solid grey;
        box-shadow: 10px 10px 5px 5px rgb(239, 133, 133);
    }

    h1 {
        text-align: center;
    }

    p {
        margin-left: 50px;
        font-size: 30px;
        color: red;
    }

    button {
        width: 130px;
        height: 40px;
        /* text-align: center; */
        margin-left: 100px;
        margin-top: 100px;
    }
</style>

<body>
    <div class="name">
        <h1>随机点名</h1>

        <p>请点击开始按钮开始点名</p>
        <button class="begin">开始</button>
        <button class="end">结束</button>
    </div>
    <script>
        let arr = ['曹操', '刘备', '关羽', '张飞', '周瑜', '赵云', '黄忠', '成吉思汗']
        const begin = document.querySelector('.begin')
        let n
        begin.addEventListener('click', function () {
            n = setInterval(function () {
                i = Math.floor(Math.random() * arr.length)

                const pp = document.querySelector('p')
                pp.innerHTML = `${arr[i]}`
                if (arr.length === 1) {
                    begin.disabled = true
                    end  .disabled = true
                }

            }, 100)

        })
        const end = document.querySelector('.end')
        end.addEventListener('click', function () {
            clearInterval(n)
            arr.splice(i, 1)

        })

    </script>
</body>

</html>